package lt.marsrutai.mobile.android.activity;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.StopMapIntentHolder;
import lt.marsrutai.mobile.android.manager.ActivityManager;
import lt.marsrutai.mobile.android.map.GetStopsAsyncTask;
import lt.marsrutai.mobile.android.map.overlay.StopBalloonView;
import lt.marsrutai.mobile.android.map.overlay.StopOverlayItem;
import lt.marsrutai.mobile.android.map.overlay.TappableOverlayContainer;
import lt.marsrutai.mobile.android.model.Stop;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.Overlay;

public class StopMapActivity extends BaseMapActivity {

    private ActivityManager manager;
    private StopMapIntentHolder intentHolder;
    private List<Stop> stops;
    private AsyncTask<StopMapIntentHolder, Integer, List<Stop>> asyncTask;

    public StopMapActivity() {
        super(false);
        manager = new ActivityManager(this);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        receiveItemData();
        asyncTask = downloadData();
    }

    private Runnable returnRes = new Runnable() {

        @Override
        public void run() {
            try {
                stops = asyncTask.get();
            } catch (CancellationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            if (stops != null && stops.size() > 0) {
                List<Overlay> mapOverlays = mapView.getOverlays();

                TappableOverlayContainer<StopOverlayItem> stopOverlay = new TappableOverlayContainer<StopOverlayItem>(mapView, getResources().getDrawable(R.drawable.all_stops), new StopBalloonView(mapView.getContext()));
                List<GeoPoint> geoPoints = new LinkedList<GeoPoint>();
                
                for (Stop stop : stops) {

                    if (null == stop.getLatitude() || null == stop.getLongitude()) {
                        continue;
                    }

                    GeoPoint point = createGeoPoint(stop.getLatitude(), stop.getLongitude());
                    geoPoints.add(point);
                    StopOverlayItem overlayItem = new StopOverlayItem(intentHolder.getScopeFolder(), point, stop);

                    if (stops.indexOf(stop) == 0) {
                        overlayItem.setMarker(getStopMarker(R.drawable.first_stop));
                    }
                    else if (stops.indexOf(stop) == stops.size() - 1) {
                        overlayItem.setMarker(getStopMarker(R.drawable.last_stop));
                    }

                    stopOverlay.addOverlay(overlayItem);
                }

                if (geoPoints.size() == 0) {
                    manager.makeToast(StopMapActivity.this, getString(stops.size() > 1 ? R.string.track_location_unknown
                            : R.string.stop_location_unknown));
                }
                else {
                    mapOverlays.add(stopOverlay);
                    
                    if (geoPoints.size() > 1) {
                        // Center map and zoom in to the route
                        zoomToItems(geoPoints);
                    }
                    else {
                        MapController mapController = mapView.getController();
                        mapController.setCenter(geoPoints.get(0));
                    }

                }
            }
        }
    };
    
    public Runnable getAsyncTaskResultProcessor() {
        return returnRes;
    }

    private Drawable getStopMarker(int drawableId) {
        Drawable marker = getResources().getDrawable(drawableId);
        return TappableOverlayContainer.boundCenterBottom(marker);
    }
    
    public void receiveItemData() {
        intentHolder = new StopMapIntentHolder(getIntent());
    }

    public AsyncTask<StopMapIntentHolder, Integer, List<Stop>> downloadData() {
        return new GetStopsAsyncTask(this).execute(intentHolder);
    }

}
